package com.dehui.traders.ui.market.rankdetail;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.HorizontalScrollView;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.adapter.TableAdapter;
import com.dehui.traders.bean.PlateListBean;
import com.dehui.traders.config.Config;
import com.dehui.traders.databinding.ActivityRankDetailBinding;
import com.dehui.traders.ui.base.AppActvity;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.widgets.MyHScrollView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;


public class RankDetailActivity extends AppActvity<ActivityRankDetailBinding,RankDetailViewModel> implements TableAdapter.OnTableItemClick {

    public static final String IntentKey_TypeCode = "IntentKey_TypeCode";
    public static final String IntentKey_Name = "IntentKey_Name";
    private String typeCode;
    private int page = 0;
    public HorizontalScrollView mTouchView;
    private TheRankDetailAdapter adapter;
    List<PlateListBean.PlateData> mList = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;
    private String name;

    @Override
    public int initContentView() {
        return R.layout.activity_rank_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public RankDetailViewModel initViewModel() {
        return new RankDetailViewModel(this);
    }

    public static void start(Context context,String typeCode,String name){
        Intent intent = new Intent(context,RankDetailActivity.class);
        intent.putExtra(IntentKey_TypeCode,typeCode);
        intent.putExtra(IntentKey_Name,name);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        typeCode = getIntent().getStringExtra(IntentKey_TypeCode);
        name = getIntent().getStringExtra(IntentKey_Name);
        initToolbar("市场", name);
        viewModel.tradeupdownlist(typeCode,page);
        initViews();
    }

    private void initViews() {
        refreshLayout = findViewById(R.id.sr_refresh);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setEnableLoadmore(true);
//        refreshLayout.autoLoadmore();

        adapter = new TheRankDetailAdapter(this, mList, this);
        binding.tabView.setFixes(Config.indexTitle)
                .setOthers(Config.rankTitle)
                .setActivity(this)
                .setOnItemClick(this)
                .setAdapter(adapter).build();

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 0;
                viewModel.tradeupdownlist(typeCode,page);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page ++;
                viewModel.tradeupdownlist(typeCode,page);
            }
        });

    }

    public void updataList(List<PlateListBean.PlateData> list) {
        if (page == 0){
            mList.clear();
        }
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        mList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        List<MyHScrollView> mHScrollViews = binding.tabView.getScrollViews();
        for(MyHScrollView scrollView : mHScrollViews) {
            if(mTouchView != scrollView)
                scrollView.smoothScrollTo(l, t);
        }
    }

    @Override
    public void onItemClick(int position) {
        PlateListBean.PlateData plateData = mList.get(position);
        StocksDetailActivity.start(this,name,plateData.name,
                plateData.code.substring(2),String.valueOf(plateData.closePrice),plateData.code.substring(0,2));
    }


}
